package com.kun.service.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kun.domain.TUserInfo;
import com.kun.service.mapper.TUserInfoMapper;
import com.kun.service.service.ITUserInfoService;
import com.kun.domain.TRolePermission;
import com.kun.service.util.UserContext;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;

@Service
public class TUserInfoServiceImpl extends ServiceImpl<TUserInfoMapper, TUserInfo> implements ITUserInfoService {

    /**
     * 新增用户信息
     */
    @Override
    public boolean insert(TUserInfo tUserInfo) {
        return save(tUserInfo);
    }

    /**
     * 根据用户ID查找用户信息
     */
    @Override
    public TUserInfo selectByUserId(Long userId) {
        return lambdaQuery().eq(TUserInfo::getUserId, userId).last("LIMIT 1").one();
    }

    /**
     * 修改用户信息
     */
    @Override
    public Boolean updateUserInfo(TUserInfo tUserInfo) {
        Long userId = UserContext.getUserId();
        TUserInfo tUserInfo1 = selectByUserId(userId);
        Assert.isTrue(tUserInfo1 != null, "当前用户不存在");
        tUserInfo.setId(tUserInfo1.getId());
        return updateById(tUserInfo);
    }

    /**
     * 根据用户ID查询权限
     */
    @Override
    public List<TRolePermission> queryPermissionByUserId(Long id) {
        return baseMapper.queryPermissionByUserId(id);
    }
}
